package com.pospi.pospihold.ui.login_ui;

import android.app.Dialog;
import android.content.SharedPreferences;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.pospi.pospihold.R;
import com.pospi.pospihold.dialog.WaitingDialog;
import com.pospi.pospihold.dto.ReturnDto;
import com.pospi.pospihold.http.LoadServer;
import com.pospi.pospihold.ui.base.BaseActivity;
import com.pospi.pospihold.util.Api;

import org.apache.http.Header;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {

    @Bind(R.id.user_name)
    EditText userName;
    @Bind(R.id.et_pwd)
    EditText etPwd;
    @Bind(R.id.btn_login)
    Button btnLogin;

    private Dialog dialog;
    private String token;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

    }

    @OnClick(R.id.btn_login)
    public void onClick() {
        if (userName.getText().toString().isEmpty()) {
            Toast.makeText(LoginActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
        } else if (etPwd.getText().toString().isEmpty()) {
            Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
        } else {
            showDialog("正在登陆");
            RequestParams params = new RequestParams();
            params.put("Email", userName.getText().toString().trim());
            params.put("Password", etPwd.getText().toString().trim());
            params.put("Imei", getPhoneMac());

            LoadServer.getConnect(getApplicationContext(), Api.LOGIN, params, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int i, Header[] headers, byte[] bytes) {
                    Log.i("bytes", new String(bytes));
                    Gson gson = new Gson();
                    ReturnDto dto = gson.fromJson(new String(bytes), ReturnDto.class);
                    closeDialog();
                    if (dto.getResult() == 1) {
                        String[] names = dto.getValue().split("\\,");
                        token = names[0] + "," + names[1];//得到token的值

                        SharedPreferences.Editor editor = getSharedPreferences("Token", MODE_PRIVATE).edit();
                        editor.putString("value", dto.getValue());//返回的value
                        editor.putString("userName", userName.getText().toString());//登陆的账号的信息
                        editor.putString("token", names[0] + "," + names[1]);//把token的值进行存储起来
                        editor.apply();
                        startActivity(SelectStoreActivity.class);
                        finish();
                    } else {
                        showToast(dto.getMessage());
                    }

                }

                @Override
                public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

                }
            });
        }
    }

    /**
     * 获取登录手机的Mac。但是该方法必须要连接上了wifi才可以
     *
     * @return
     */
    public String getPhoneMac() {
        String macAddress = "000000000000";
        try {
            //首先得到系统的服务
            WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
            WifiInfo info = (null == wifiManager ? null : wifiManager.getConnectionInfo());
            //当info 不是为空的时候
            if (info != null) {
                //当info.getMacAddress不为空的时候
                if (!TextUtils.isEmpty(info.getMacAddress())) {
                    macAddress = info.getMacAddress().replace(":", "");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return macAddress;
    }

    public void showDialog(String msg) {
        if (dialog == null) {
            dialog = WaitingDialog.createLoadingDialog(LoginActivity.this, msg);
            dialog.show();
        }
    }

    public void closeDialog() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }
}
